package com.swust.exam.controller;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.PathVariable;

import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestMapping;



import com.swust.exam.pojo.Course;
import com.swust.exam.service.CourseService;

@Controller
@RequestMapping("/course")
public class CourseController {
	
	@Autowired
	private CourseService courseService;
	
	
	@RequestMapping(value="/get")
	public Course courseGet(Integer id) {
		return courseService.get(id);
	}
	
	@RequestMapping(value="/delete")
	public String courseDelete(Integer id ,Model model) {
		 courseService.delete(id);
		 List<Course> list=courseService.courselist();
		 model.addAttribute("courselist",list);
		return "course";
	}
	
	@RequestMapping(value="/insert")
	
	public String courseInsert(Course course,Model model) {
		/*List<Course> list=courseService.courselist();
		boolean flag=false;
		for(int i = 0 ; i < list.size() ; i++) {
			  if(list.get(i).getCourseId()==course.getCourseId()
			    ||list.get(i).getCourseNumber().equals(course.getCourseNumber())==true) {
				  flag=true;
				  break;
			  }
			}
		if(flag==false ) {
		 courseService.insert(course);
		 model.addAttribute("courselist",list);
		}
		else {
		}
		return "course";
	}*/
		
		 courseService.insert(course);
		 List<Course> list=courseService.courselist();
		 model.addAttribute("courselist",list);
		 return "course";
	
	}
	 
	@PutMapping(value="/update")
	public int courseUpdate(Course course){
		return courseService.update(course);
	}
	

}
